package com.yuanli.cosmeticmirror.view.activity;

import android.os.Bundle;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;
import com.yuanli.cosmeticmirror.base.MyApp;
import com.yuanli.cosmeticmirror.bean.PhotoFrame;
import com.yuanli.cosmeticmirror.bean.PhotoFrameBean;
import com.yuanli.cosmeticmirror.bean.PhotoFrameStatus;
import com.yuanli.cosmeticmirror.model.iview.IPhotoFrameView;
import com.yuanli.cosmeticmirror.persenter.PhotoFramePersenter;
import com.yuanli.cosmeticmirror.view.adapter.FmPagerAdapter;
import com.yuanli.cosmeticmirror.view.fragment.TabFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 相框
 */
public class PhotoFrameActivity extends BaseActivity implements IPhotoFrameView {
    @BindView(R.id.photo_frame_back)
    ImageView photoFrameBack;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;


    private FmPagerAdapter pagerAdapter;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private PhotoFramePersenter persenter;

    @Override
    protected int getContentView() {
        return R.layout.activity_photo_frame;
    }

    @Override
    protected void init(Bundle bundle) {
        persenter = new PhotoFramePersenter(this, this);
    }

    @Override
    protected void initView() {
        persenter.getPhotoFrame();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void destroy() {

    }

    @OnClick(R.id.photo_frame_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void sucess(PhotoFrame object) {
        for (int i = 0; i < object.getData().size(); i++) {
            titles.add(object.getData().get(i).getTitle());
            fragments.add(new TabFragment(i, object.getData().get(i).getId()));
            tabLayout.addTab(tabLayout.newTab());
        }
        tabLayout.setupWithViewPager(viewPager, false);
        pagerAdapter = new FmPagerAdapter(fragments, titles, getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        if (MyApp.getPhotoFrameStatus().fragmentIndex != -1) {
            viewPager.setCurrentItem(MyApp.getPhotoFrameStatus().fragmentIndex);
        }
    }

    @Override
    public void error(String msg) {
        showToast(msg);
    }

    @Override
    public void getPhotoFrame(List<PhotoFrameBean> object) {

    }
}
